package pcmd_item;

use strict;
use warnings;



#-------
# New --
#-------
sub new {
    my $arg0 = $_[0];
    my $cls = ref($arg0) || $arg0;
    
    my $self = {};
    bless $self, $cls;

    $self->init($_[1]);
    
    return $self;
}


#--------------
# Init class --
#--------------
sub init {
    my $self = shift;
    my $init_hash = shift;

    foreach my $key (keys %{$init_hash}) {
        $self->{$key} = $init_hash->{$key};
    };
    
    if ($self->{type} eq 'file') {
        #print "$self->{abs_path}\n";
        $self->update_stat();     
    }; 
    
}


#-------------
# Get field --
#-------------
sub get_field {
    my $self = shift;
    my $field_name = shift;
    
    return $self->{$field_name};
};


#------------
# Get type --
#------------
sub get_type {
    my $self = shift;
    
    if ($self->{'type'} eq 'dir') {
        return 'dir';
    } else {
        return 'file';
    };
};


#------------
# Get name --
#------------
sub get_name {
    my $self = shift;
    
    if ($self->{'type'} eq 'dir') {
        return $self->{'name'};
    } else {
        return $self->{name} . $self->{ext};
    };
};


#--------------
# Print stat --
#--------------
sub print_stat {
    my $self = shift;
    
    print "-- $self->{abs_path}: | ";
#    print "mtime: $self->{mtime} | ";
#    print "size: $self->{size} | ";
#    print "mode: $self->{mode} | ";
    print "\n";
};


#---------------
# Update stat --
#---------------
sub update_stat {
    my $self = shift;
    
    my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
       $atime, $mtime, $ctime, $blksize, $blocks) = stat($self->{abs_path});
       
    $self->{mtime} = convert_to_date($mtime);
    $self->{size} = convert_size_to_string($size);
    $self->{mode} = sprintf("%04o", $mode & 07777);
};


#-------------------
# Convert to date --
#-------------------
sub convert_to_date {
    my $time_in_sec = shift;
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time_in_sec);
    my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
    $year += 1900;
        
    return sprintf("%02d:%02d %02d %s %d", $hour, $min, $mday, $abbr[$mon], $year );
};


#--------------------------
# Convert size to string --
#--------------------------
sub convert_size_to_string {
    my $size = shift;
    
    my $tmp = $size / 1024;
    
    if ($tmp < 1) {
        return "$size B";
    } else {
        $size = $tmp;
        $tmp = $size / 1024;
        
        if ($tmp < 1) {
            return  sprintf("%.2f kB", $size);
        } else {
            return  sprintf("%.2f MB", $tmp);
        };
    };
    
};


#-----------
# Compare --
#-----------
sub compare {
    my $self = shift;
    my $other = shift;
    my $base =  shift;

    unless (defined $base) { $base = 'name' };

    if (($self->{'type'} eq 'dir') and ($other->{'type'} eq 'dir')) {
        return $self->{'name'} cmp $other->{'name'};
    } elsif ($self->{'type'} eq 'dir') {
        return -1;
    } elsif ($other->{'type'} eq 'dir') {
        return 1;
    } else {
        #print "$base $self->{$base} cmp $other->{$base}\n";
        return $self->{$base} cmp $other->{$base};
    };
};


1;

